package doc.ic.walkmore.GUI;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;

import doc.ic.walkmore.R;

import android.content.Context;
import android.os.Handler;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ToggleButton;

public class GUIManager {
	
	private Context mContext;
	
	private LinearLayout mainView;
	
	// All views
	private TextView tvStepCountTotal;
	private TextView tvDistanceTotal;
	private TextView tvDate;
	private LinearLayout summary;
	private ToggleButton mToggleButton;
	
	// to postpone some tasks..
	Handler handler = new Handler();
	
	// Parameter of the view
	SimpleDateFormat dayFormating = new SimpleDateFormat("yyyy-MM-dd");
	private String dayToDisplay;
	
	public GUIManager(Context context, LinearLayout main) {
		mContext = context;
		mainView = main;
		initialize();
	}

	private void initialize() {
		tvStepCountTotal = (TextView) mainView.findViewById(R.id.tv_stepcount);
		tvDistanceTotal = (TextView) mainView.findViewById(R.id.tv_distance);
		tvDate = (TextView) mainView.findViewById(R.id.date);
		dayToDisplay = dayFormating.format(new Date()); // TODAY
		tvDate.setText(dayToDisplay);
		summary = (LinearLayout) mainView.findViewById(R.id.summaryLayout);
		mToggleButton = (ToggleButton) mainView.findViewById(R.id.toggleButton1);
	}

	public void setDayToDisplay(String newDay) {
		dayToDisplay = newDay;
	}


	public String getDateToDisplay() {
		return dayToDisplay;
	}

	/**
	 * Updates the Gui for the DAY to display
	 * @return 1 if successful, 0 if no data for this day
	 */
	public int update(HashMap<String, Pair<Long, Long>> rowContent) {
		long totalSteps = 0, totalDistance = 0;
		if (rowContent.isEmpty())
			return 0;
		// Update Summary table
		summary.removeAllViews();
		TableLayout tableSummary = (TableLayout) LayoutInflater.from(mContext).inflate(R.layout.table_summary, null);
		summary.addView(tableSummary);
		for (Entry<String, Pair<Long, Long>> activity : rowContent.entrySet()) {
			TableRow row = (TableRow) LayoutInflater.from(mContext).inflate(R.layout.table_row, null);
			TextView tv1 = (TextView) row.getChildAt(0);
			tv1.setText(activity.getKey());
			TextView tv2 = (TextView) row.getChildAt(1);
			long steps = activity.getValue().first;
			tv2.setText(steps+"");
			TextView tv3 = (TextView) row.getChildAt(2);
			long distance = activity.getValue().second;
			tv3.setText(distance/100+"m");
			tableSummary.addView(row);
			totalSteps += steps;
			totalDistance += distance;
		}
		// Update TextViews
		tvDistanceTotal.setText(totalDistance/100+"m");
		tvStepCountTotal.setText(totalSteps+"");
		tvDate.setText(dayToDisplay);
		// And let's flash parents layout, so that user noticies it..
		View parent = (View) tvDistanceTotal.getParent();
//		flash(parent);
		
		return 1;		
	}

	// To be used only with the relative layout which contains distance/stepcount totals
	private void flash(final View parent) {
		Runnable r = new Runnable() {			
			@Override
			public void run() {parent.setBackgroundResource(R.drawable.box);}
		};
		parent.setBackgroundResource(R.drawable.box_flash);
		handler.postDelayed(r, 300);		
	}

	public void uncheckToggleButton() {
		if (mToggleButton.isChecked())
			mToggleButton.toggle();
	}

	public void checkToggleButton() {
		if (!mToggleButton.isChecked())
			mToggleButton.toggle();		
	}
	
}
